package com.base.util.demo.jniutils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.base.util.demo.DemoBaseActivity;
import com.base.util.demo.R;
import com.base.util.javautils.app.BaseActivity;
import com.base.util.jniutils.JniBase64;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_base64)
public class Base64Activity extends DemoBaseActivity {

    public static void show(DemoBaseActivity act) {
        act.startActivity(new Intent(act, Base64Activity.class));
    }

    @ViewInject(R.id.et_base64)
    private EditText et_base64;
    @ViewInject(R.id.btn_encode)
    private Button btn_encode;
    @ViewInject(R.id.btn_decode)
    private Button btn_decode;
    @ViewInject(R.id.btn_unittest)
    private Button btn_unittest;
    @ViewInject(R.id.btn_clear)
    private Button btn_clear;
    @ViewInject(R.id.btn_copy)
    private Button btn_copy;
    @ViewInject(R.id.btn_paste)
    private Button btn_paste;
    @ViewInject(R.id.btn_double)
    private Button btn_double;
    @ViewInject(R.id.btn_clear_edit)
    private Button btn_clearEdit;
    @ViewInject(R.id.cb_flag_nopadding)
    private CheckBox cb_noPadding;
    @ViewInject(R.id.cb_flag_nowrap)
    private CheckBox cb_noWrap;
    @ViewInject(R.id.cb_flag_crlf)
    private CheckBox cb_crlf;
    @ViewInject(R.id.cb_flag_url)
    private CheckBox cb_urlSafe;

    private int m_iFlags = JniBase64.DEFAULT;

    private String m_strCopy;

    private final SparseIntArray mArrayFlags = new SparseIntArray();

    {
        mArrayFlags.put(R.id.cb_flag_nopadding, JniBase64.NO_PADDING);
        mArrayFlags.put(R.id.cb_flag_nowrap, JniBase64.NO_WRAP);
        mArrayFlags.put(R.id.cb_flag_crlf, JniBase64.CRLF);
        mArrayFlags.put(R.id.cb_flag_url, JniBase64.URL_SAFE);
    }

    private final OnClickListener mOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                switch (v.getId()) {
                    case R.id.btn_encode:
                        encode();
                        break;
                    case R.id.btn_decode:
                        decode();
                        break;
                    case R.id.btn_clear: {
                        cb_noPadding.setChecked(false);
                        cb_noWrap.setChecked(false);
                        cb_crlf.setChecked(false);
                        cb_urlSafe.setChecked(false);
                    }
                    break;
                    case R.id.btn_copy:
                        m_strCopy = et_base64.getText().toString();
                        break;
                    case R.id.btn_paste:
                        et_base64.setText(m_strCopy);
                        break;
                    case R.id.btn_double: {
                        Editable text = et_base64.getText();
                        text.append(text);
                    }
                    break;
                    case R.id.btn_clear_edit:
                        et_base64.setText("");
                        break;
                    case R.id.btn_unittest:
                        unitTest();
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private final OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int flags = mArrayFlags.get(buttonView.getId());
            if (isChecked) {
                m_iFlags |= flags;
            } else {
                m_iFlags &= (~flags);
            }
            Log.i("base64", "" + isChecked + " flags " + m_iFlags);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btn_encode.setOnClickListener(mOnClickListener);
        btn_decode.setOnClickListener(mOnClickListener);
        btn_unittest.setOnClickListener(mOnClickListener);
        btn_clear.setOnClickListener(mOnClickListener);
        btn_copy.setOnClickListener(mOnClickListener);
        btn_paste.setOnClickListener(mOnClickListener);
        btn_double.setOnClickListener(mOnClickListener);
        btn_clearEdit.setOnClickListener(mOnClickListener);

        cb_noPadding.setOnCheckedChangeListener(mOnCheckedChangeListener);
        cb_noWrap.setOnCheckedChangeListener(mOnCheckedChangeListener);
        cb_crlf.setOnCheckedChangeListener(mOnCheckedChangeListener);
        cb_urlSafe.setOnCheckedChangeListener(mOnCheckedChangeListener);
    }

    private void encode() {
        StringBuilder stringBuilder = new StringBuilder();
        String content = et_base64.getText().toString();
        byte[] bs = content.getBytes();
        bs = JniBase64.encode(bs, m_iFlags);

        stringBuilder.append("Base64 encode: ");
        content = new String(bs);
        stringBuilder.append(content);

        stringBuilder.append("\nAndroid base64 decode: ");
        bs = android.util.Base64.decode(bs, m_iFlags);
        content = new String(bs);
        stringBuilder.append(content);

        et_base64.setText(stringBuilder);
    }

    private void decode() {
        StringBuilder stringBuilder = new StringBuilder();
        String content = et_base64.getText().toString();
        byte[] bs = content.getBytes();
        bs = android.util.Base64.encode(bs, m_iFlags);

        stringBuilder.append("Android base64 encode: ");
        content = new String(bs);
        stringBuilder.append(content);

        stringBuilder.append("\nBase64 decode: ");
        bs = JniBase64.decode(bs);
        content = new String(bs);
        stringBuilder.append(content);

        et_base64.setText(stringBuilder);
    }

    private void unitTest() {
        long timeStart;
        long timeEnd;
        StringBuilder stringBuilder = new StringBuilder();
        byte[] bs = et_base64.getText().toString().getBytes();
        stringBuilder.append("Test start \nTest size --> " + bs.length);

        timeStart = System.currentTimeMillis();
        for (int i = 0; i < 1000000; ++i) {
            bs = android.util.Base64.encode(bs, m_iFlags);
            bs = android.util.Base64.decode(bs, m_iFlags);
        }
        timeEnd = System.currentTimeMillis();
        stringBuilder.append("\nJava test end --> time = " + (timeEnd - timeStart));

        timeStart = System.currentTimeMillis();
        for (int i = 0; i < 1000000; ++i) {
            bs = JniBase64.encode(bs, m_iFlags);
            bs = JniBase64.decode(bs);
        }
        timeEnd = System.currentTimeMillis();
        stringBuilder.append("\nNative test end --> time = " + (timeEnd - timeStart));

        et_base64.setText(stringBuilder);
    }

}
